package com.example.administrator.xiaobo.fragment.LiveDetailsFragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.adapter.MyRecyclerViewAdapter;
import com.example.administrator.xiaobo.bean.VarietyEntrity;
import com.example.administrator.xiaobo.callback.DataCallBackListener;
import com.example.administrator.xiaobo.url.URLAdress;
import com.example.administrator.xiaobo.utils.VarietyTask;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class VarietyFragment extends Fragment implements DataCallBackListener, SwipeRefreshLayout.OnRefreshListener,MyRecyclerViewAdapter.OnChildClickListener{

    private List<VarietyEntrity> data=new ArrayList<>();
    private RecyclerView recyclerView;
    private SwipeRefreshLayout sw;
    MyRecyclerViewAdapter adapter;
    private View view;
    private final String url = "http://hilive.baidu.com/service/api.do?ac=varitetyShowList&pi=1&ps=20&tt=";

    public VarietyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_variety,null);
        init(view);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view;

    }

    private void init(View view) {
        sw = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
       sw.setOnRefreshListener(this);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        recyclerView.setItemViewCacheSize(10);

        String BaseUrl = url + URLAdress.VARIETY_URL;
        new VarietyTask(this).execute(BaseUrl);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MyRecyclerViewAdapter(getContext(), data);
        adapter.setListener(this);
        recyclerView.setAdapter(adapter);

    }
    @Override
    public void dataCallBackListener(List<VarietyEntrity> datas) {
        this.data.addAll(datas);
        adapter.notifyDataSetChanged();
        sw.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        data.clear();
        Long time = System.currentTimeMillis();
        String BaseUrl = url + time;
        new VarietyTask(this).execute(BaseUrl);
    }


    @Override
    public void onClick(int position, String id, String rep) {

        Toast.makeText(getContext(), ""+position+id+rep, Toast.LENGTH_SHORT).show();
    }
}
